<!DOCTYPE html>
<html lang="zh-CN">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <meta name="robots" content="noodp" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge, chrome=1">
        <title>设计模式--单例模式 - 豆是不吃豆豉</title><meta name="Description" content="单例模式"><meta property="og:title" content="设计模式--单例模式" />
<meta property="og:description" content="单例模式" />
<meta property="og:type" content="article" />
<meta property="og:url" content="https://dou-chi.gitee.io/design-patterns-singleton/" />
<meta property="og:image" content="https://dou-chi.gitee.io/logo.png"/>
<meta property="article:published_time" content="2020-09-01T19:21:27+08:00" />
<meta property="article:modified_time" content="2020-09-01T19:21:27+08:00" />
<meta name="twitter:card" content="summary_large_image"/>
<meta name="twitter:image" content="https://dou-chi.gitee.io/logo.png"/>

<meta name="twitter:title" content="设计模式--单例模式"/>
<meta name="twitter:description" content="单例模式"/>
<meta name="application-name" content="豆是不吃豆豉">
<meta name="apple-mobile-web-app-title" content="豆是不吃豆豉"><meta name="theme-color" content="#ffffff"><meta name="msapplication-TileColor" content="#da532c"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico" />
        <link rel="icon" type="image/png" sizes="32x32" href="/favicon-32x32.png">
        <link rel="icon" type="image/png" sizes="16x16" href="/favicon-16x16.png"><link rel="apple-touch-icon" sizes="180x180" href="/apple-touch-icon.png"><link rel="mask-icon" href="/safari-pinned-tab.svg" color="#5bbad5"><link rel="manifest" href="/site.webmanifest"><link rel="canonical" href="https://dou-chi.gitee.io/design-patterns-singleton/" /><link rel="prev" href="https://dou-chi.gitee.io/redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6/" /><link rel="next" href="https://dou-chi.gitee.io/activiti%E4%B8%80%E7%AE%80%E4%BB%8B/" /><link rel="stylesheet" href="/lib/normalize/normalize.min.css"><link rel="stylesheet" href="/css/style.min.css"><link rel="stylesheet" href="/lib/fontawesome-free/all.min.css"><link rel="stylesheet" href="/lib/animate/animate.min.css"><script type="application/ld+json">
    {
        "@context": "http://schema.org",
        "@type": "BlogPosting",
        "headline": "设计模式--单例模式",
        "inLanguage": "zh-CN",
        "mainEntityOfPage": {
            "@type": "WebPage",
            "@id": "https:\/\/dou-chi.gitee.io\/design-patterns-singleton\/"
        },"genre": "posts","keywords": "设计模式, 单例模式","wordcount":  1049 ,
        "url": "https:\/\/dou-chi.gitee.io\/design-patterns-singleton\/","datePublished": "2020-09-01T19:21:27+08:00","dateModified": "2020-09-01T19:21:27+08:00","publisher": {
            "@type": "Organization",
            "name": ""},"author": {
                "@type": "Person",
                "name": "豆是不吃豆豉"
            },"description": "单例模式"
    }
    </script></head>
    <body header-desktop="fixed" header-mobile="auto"><script type="text/javascript">(window.localStorage && localStorage.getItem('theme') ? localStorage.getItem('theme') === 'dark' : ('dark' === 'auto' ? window.matchMedia('(prefers-color-scheme: dark)').matches : 'dark' === 'dark')) && document.body.setAttribute('theme', 'dark');</script>

        <div id="mask"></div><div class="wrapper"><header class="desktop" id="header-desktop">
    <div class="header-wrapper">
        <div class="header-title">
            <a href="/" title="豆是不吃豆豉">豆是不吃豆豉</a>
        </div>
        <div class="menu">
            <div class="menu-inner"><a class="menu-item" href="/posts/"> 所有文章 </a><a class="menu-item" href="/tags/"> 标签 </a><a class="menu-item" href="/categories/"> 分类 </a><a class="menu-item" href="https://github.com/dou-chi" rel="noopener noreffer" target="_blank"> github </a><a class="menu-item" href="https://gitee.com/dou-chi" rel="noopener noreffer" target="_blank"> gitee </a><span class="menu-item delimiter"></span><span class="menu-item search" id="search-desktop">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-desktop">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-desktop" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-desktop" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-desktop">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </span><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                    <i class="fas fa-adjust fa-fw"></i>
                </a>
            </div>
        </div>
    </div>
</header><header class="mobile" id="header-mobile">
    <div class="header-container">
        <div class="header-wrapper">
            <div class="header-title">
                <a href="/" title="豆是不吃豆豉">豆是不吃豆豉</a>
            </div>
            <div class="menu-toggle" id="menu-toggle-mobile">
                <span></span><span></span><span></span>
            </div>
        </div>
        <div class="menu" id="menu-mobile"><div class="search-wrapper">
                    <div class="search mobile" id="search-mobile">
                        <input type="text" placeholder="搜索文章标题或内容..." id="search-input-mobile">
                        <a href="javascript:void(0);" class="search-button search-toggle" id="search-toggle-mobile" title="搜索">
                            <i class="fas fa-search fa-fw"></i>
                        </a>
                        <a href="javascript:void(0);" class="search-button search-clear" id="search-clear-mobile" title="清空">
                            <i class="fas fa-times-circle fa-fw"></i>
                        </a>
                        <span class="search-button search-loading" id="search-loading-mobile">
                            <i class="fas fa-spinner fa-fw fa-spin"></i>
                        </span>
                    </div>
                    <a href="javascript:void(0);" class="search-cancel" id="search-cancel-mobile">
                        取消
                    </a>
                </div><a class="menu-item" href="/posts/" title="">所有文章</a><a class="menu-item" href="/tags/" title="">标签</a><a class="menu-item" href="/categories/" title="">分类</a><a class="menu-item" href="https://github.com/dou-chi" title="" rel="noopener noreffer" target="_blank">github</a><a class="menu-item" href="https://gitee.com/dou-chi" title="" rel="noopener noreffer" target="_blank">gitee</a><a href="javascript:void(0);" class="menu-item theme-switch" title="切换主题">
                <i class="fas fa-adjust fa-fw"></i>
            </a></div>
    </div>
</header>
<div class="search-dropdown desktop">
    <div id="search-dropdown-desktop"></div>
</div>
<div class="search-dropdown mobile">
    <div id="search-dropdown-mobile"></div>
</div>
<main class="main">
                <div class="container"><div class="toc" id="toc-auto">
            <h2 class="toc-title">目录</h2>
            <div class="toc-content always-active" id="toc-content-auto"></div>
        </div><article class="page single"><h1 class="single-title animated flipInX">设计模式--单例模式</h1><div class="post-meta">
            <div class="post-meta-line"><span class="post-author"><a href="/" title="Author" rel=" author" class="author"><i class="fas fa-user-circle fa-fw"></i>豆是不吃豆豉</a></span>&nbsp;<span class="post-category">收录于 <a href="/categories/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/"><i class="far fa-folder fa-fw"></i>设计模式</a></span></div>
            <div class="post-meta-line"><i class="far fa-calendar-alt fa-fw"></i>&nbsp;<time datetime="2020-09-01">2020-09-01</time>&nbsp;<i class="fas fa-pencil-alt fa-fw"></i>&nbsp;约 1049 字&nbsp;
                <i class="far fa-clock fa-fw"></i>&nbsp;预计阅读 3 分钟&nbsp;</div>
        </div><div class="details toc" id="toc-static"  kept="">
                <div class="details-summary toc-title">
                    <span>目录</span>
                    <span><i class="details-icon fas fa-angle-right"></i></span>
                </div>
                <div class="details-content toc-content" id="toc-content-static"><nav id="TableOfContents">
  <ul>
    <li>
      <ul>
        <li><a href="#特点">特点</a></li>
        <li><a href="#实现">实现</a>
          <ul>
            <li><a href="#饿汉式">饿汉式</a></li>
            <li><a href="#双重检查">双重检查</a></li>
            <li><a href="#静态内部类">静态内部类</a></li>
            <li><a href="#枚举">枚举</a></li>
          </ul>
        </li>
      </ul>
    </li>
  </ul>
</nav></div>
            </div><div class="content" id="content"><h2 id="特点">特点</h2>
<ul>
<li>全局只有一个实例</li>
<li>内部自我创建实例</li>
<li>对外提供获取实例方法</li>
</ul>
<h2 id="实现">实现</h2>
<h3 id="饿汉式">饿汉式</h3>
<p>在类加载时就初始化实例，是线程安全的，但是这种方式不管该实例有没有被使用到，都会被实例化，存在浪费空间的情况。所以我们可以考虑使用饿汉式的方式。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Java" data-lang="Java">    <span class="kd">public</span> <span class="kd">class</span> <span class="nc">HungrySingleton</span> <span class="o">{</span>

    <span class="cm">/**
</span><span class="cm">     * 类初始化时就加载到内存中
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="n">HungrySingleton</span> <span class="n">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">HungrySingleton</span><span class="o">();</span>

    <span class="kd">private</span> <span class="nf">HungrySingleton</span><span class="o">(){</span>

    <span class="o">}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">HungrySingleton</span> <span class="nf">getInstance</span><span class="o">(){</span>
        <span class="k">return</span> <span class="n">instance</span><span class="o">;</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="双重检查">双重检查</h3>
<p>我们只在类使用时才创建对象，自然而然相当以下的实现方式，但是这种方式会存在线程安全问题，因为在多线程情况下，可能会创建出多个对象。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Java" data-lang="Java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Singleton</span> <span class="o">{</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="n">Singleton</span> <span class="n">instance</span><span class="o">;</span>

    <span class="cm">/**
</span><span class="cm">     * 构造私有化
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="nf">Singleton</span><span class="o">(){}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">Singleton</span> <span class="nf">getInstance</span><span class="o">(){</span>
        <span class="c1">// 当实例为空时才会创建对象
</span><span class="c1"></span>        <span class="k">if</span> <span class="o">(</span><span class="n">instance</span> <span class="o">==</span> <span class="kc">null</span><span class="o">){</span>
            <span class="n">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Singleton</span><span class="o">();</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="n">instance</span><span class="o">;</span>
    <span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>对于上述情况，我们可以给 <code>getInstance()</code> 使用 <code>synchronized</code> 进行加锁，这样就保证了线程安全。但是每次获取实例，都要先获取锁，而加锁的目的是防止在创建对象时出现线程安全问题创建出多个实例，那么我们可不可以再真正实例化的时候再同步呢？</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Java" data-lang="Java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">DoubleCheckSingleton</span> <span class="o">{</span>

    <span class="cm">/**
</span><span class="cm">     * volatile 保持线程可见性，防止【指令重排】
</span><span class="cm">     *
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">volatile</span> <span class="n">DoubleCheckSingleton</span> <span class="n">instance</span><span class="o">;</span>

    <span class="cm">/**
</span><span class="cm">     * 构造私有化
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="nf">DoubleCheckSingleton</span><span class="o">(){}</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">DoubleCheckSingleton</span> <span class="nf">getInstance</span><span class="o">(){</span>
        <span class="c1">// 进入代码先判断是否已经实例化
</span><span class="c1"></span>        <span class="k">if</span> <span class="o">(</span><span class="n">instance</span> <span class="o">==</span> <span class="kc">null</span><span class="o">){</span>
            <span class="c1">// 进行加锁
</span><span class="c1"></span>            <span class="kd">synchronized</span> <span class="o">(</span><span class="n">DoubleCheckSingleton</span><span class="o">.</span><span class="na">class</span><span class="o">){</span>
                <span class="c1">// 确实没有实例化
</span><span class="c1"></span>                <span class="k">if</span> <span class="o">(</span><span class="n">instance</span> <span class="o">==</span> <span class="kc">null</span><span class="o">){</span>
                    <span class="n">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">DoubleCheckSingleton</span><span class="o">();</span>
                <span class="o">}</span>
            <span class="o">}</span>
        <span class="o">}</span>
        <span class="k">return</span> <span class="n">instance</span><span class="o">;</span>
    <span class="o">}</span>

<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p><code>双重检查</code>相比于直接对方法加锁，提高了效率。此时的 <code>instance</code>属性需要用<code>volatile</code>关键字修饰，<code>volatile</code>保证了线程的可见性，<code>防止指令的重排序</code>，因为在类的初始化大致分为三步 1.分配内存、2.初始化对象、3.内存地址指向引用，由于2、3步骤没有依赖性，可能导致指令重排序后 3排在2之前，导致类实例化出现问题。</p>
<h3 id="静态内部类">静态内部类</h3>
<p>我们可以利用 JVM 的类加载机制，定义一个静态内部 <code>InstanceHolder</code> 类持有实例对象，当我们第一次显示的<code>getInstance()</code> 方法时，JVM 才会实例化instance。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt"> 1
</span><span class="lnt"> 2
</span><span class="lnt"> 3
</span><span class="lnt"> 4
</span><span class="lnt"> 5
</span><span class="lnt"> 6
</span><span class="lnt"> 7
</span><span class="lnt"> 8
</span><span class="lnt"> 9
</span><span class="lnt">10
</span><span class="lnt">11
</span><span class="lnt">12
</span><span class="lnt">13
</span><span class="lnt">14
</span><span class="lnt">15
</span><span class="lnt">16
</span><span class="lnt">17
</span><span class="lnt">18
</span><span class="lnt">19
</span><span class="lnt">20
</span><span class="lnt">21
</span><span class="lnt">22
</span><span class="lnt">23
</span><span class="lnt">24
</span><span class="lnt">25
</span><span class="lnt">26
</span><span class="lnt">27
</span><span class="lnt">28
</span><span class="lnt">29
</span><span class="lnt">30
</span><span class="lnt">31
</span><span class="lnt">32
</span><span class="lnt">33
</span><span class="lnt">34
</span><span class="lnt">35
</span><span class="lnt">36
</span><span class="lnt">37
</span><span class="lnt">38
</span><span class="lnt">39
</span><span class="lnt">40
</span><span class="lnt">41
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Java" data-lang="Java"><span class="kd">public</span> <span class="kd">class</span> <span class="nc">InnerClassSingleton</span> <span class="kd">implements</span> <span class="n">Serializable</span> <span class="o">{</span>

    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">serialVersionUID</span> <span class="o">=</span> <span class="o">-</span><span class="n">6317719715110713627L</span><span class="o">;</span>

    <span class="kd">private</span> <span class="nf">InnerClassSingleton</span><span class="o">(){};</span>

    <span class="kd">public</span> <span class="kd">static</span> <span class="n">InnerClassSingleton</span> <span class="nf">getInstance</span><span class="o">(){</span>
        <span class="k">return</span> <span class="n">InstanceHolder</span><span class="o">.</span><span class="na">instance</span><span class="o">;</span>
    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 定义静态内部类持有单例对象属性
</span><span class="cm">     * JVM 类加载机制，当静态内部类使用到时，才会去初始化
</span><span class="cm">     *
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="kd">static</span> <span class="kd">class</span> <span class="nc">InstanceHolder</span> <span class="o">{</span>

        <span class="kd">private</span> <span class="kd">static</span> <span class="n">InnerClassSingleton</span> <span class="n">instance</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InnerClassSingleton</span><span class="o">();</span>

    <span class="o">}</span>

    <span class="cm">/**
</span><span class="cm">     * 提供反序列化方法，使反序列化后对象与原来一样
</span><span class="cm">     * @return Object
</span><span class="cm">     * @throws ObjectStreamException
</span><span class="cm">     */</span>
    <span class="kd">private</span> <span class="n">Object</span> <span class="nf">readResolve</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">ObjectStreamException</span> <span class="o">{</span>
        <span class="k">return</span> <span class="n">InstanceHolder</span><span class="o">.</span><span class="na">instance</span><span class="o">;</span>
    <span class="o">}</span>



    <span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span>
        <span class="k">for</span> <span class="o">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="n">0</span><span class="o">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">100</span><span class="o">;</span> <span class="n">i</span><span class="o">++)</span> <span class="o">{</span>
            <span class="k">new</span> <span class="n">Thread</span><span class="o">(()</span> <span class="o">-&gt;</span> <span class="o">{</span>
                <span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="n">Thread</span><span class="o">.</span><span class="na">currentThread</span><span class="o">().</span><span class="na">getName</span><span class="o">()</span> <span class="o">+</span> <span class="s">&#34;\t  &#34;</span> <span class="o">+</span> <span class="n">getInstance</span><span class="o">());</span>
            <span class="o">},</span><span class="s">&#34;线程：&#34;</span> <span class="o">+</span> <span class="n">i</span><span class="o">).</span><span class="na">start</span><span class="o">();</span>

        <span class="o">}</span>
    <span class="o">}</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><h3 id="枚举">枚举</h3>
<p>由于枚举类本身就是单例，且线程安全的，同时支持反序列化机制。</p>
<div class="highlight"><div class="chroma">
<table class="lntable"><tr><td class="lntd">
<pre class="chroma"><code><span class="lnt">1
</span><span class="lnt">2
</span><span class="lnt">3
</span><span class="lnt">4
</span><span class="lnt">5
</span></code></pre></td>
<td class="lntd">
<pre class="chroma"><code class="language-Java" data-lang="Java"><span class="kd">public</span> <span class="kd">enum</span> <span class="n">EnumSingleton</span> <span class="o">{</span>

    <span class="c1">// 实例
</span><span class="c1"></span>    <span class="n">INSTANCE</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></td></tr></table>
</div>
</div><p>源码链接：<a href="https://github.com/dou-chi/design-patterns" target="_blank" rel="noopener noreffer">https://github.com/dou-chi/design-patterns</a></p>
</div><div class="post-footer" id="post-footer">
    <div class="post-info">
        <div class="post-info-line">
            <div class="post-info-mod">
                <span>更新于 2020-09-01</span>
            </div>
            <div class="post-info-license"></div>
        </div>
        <div class="post-info-line">
            <div class="post-info-md"><span>
                            <a class="link-to-markdown" href="/design-patterns-singleton/index.md" target="_blank">阅读原始文档</a>
                        </span></div>
            <div class="post-info-share">
                <span><a href="javascript:void(0);" title="分享到 Twitter" data-sharer="twitter" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式" data-hashtags="设计模式,单例模式"><i class="fab fa-twitter fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Facebook" data-sharer="facebook" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-hashtag="设计模式"><i class="fab fa-facebook-square fa-fw"></i></a><a href="javascript:void(0);" title="分享到 WhatsApp" data-sharer="whatsapp" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式" data-web><i class="fab fa-whatsapp fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Line" data-sharer="line" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式"><i data-svg-src="/lib/simple-icons/icons/line.min.svg"></i></a><a href="javascript:void(0);" title="分享到 微博" data-sharer="weibo" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式"><i class="fab fa-weibo fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Myspace" data-sharer="myspace" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式" data-description="单例模式"><i data-svg-src="/lib/simple-icons/icons/myspace.min.svg"></i></a><a href="javascript:void(0);" title="分享到 Blogger" data-sharer="blogger" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式" data-description="单例模式"><i class="fab fa-blogger fa-fw"></i></a><a href="javascript:void(0);" title="分享到 Evernote" data-sharer="evernote" data-url="https://dou-chi.gitee.io/design-patterns-singleton/" data-title="设计模式--单例模式"><i class="fab fa-evernote fa-fw"></i></a></span>
            </div>
        </div>
    </div>

    <div class="post-info-more">
        <section class="post-tags"><i class="fas fa-tags fa-fw"></i>&nbsp;<a href="/tags/%E8%AE%BE%E8%AE%A1%E6%A8%A1%E5%BC%8F/">设计模式</a>,&nbsp;<a href="/tags/%E5%8D%95%E4%BE%8B%E6%A8%A1%E5%BC%8F/">单例模式</a></section>
        <section>
            <span><a href="javascript:void(0);" onclick="window.history.back();">返回</a></span>&nbsp;|&nbsp;<span><a href="/">主页</a></span>
        </section>
    </div>

    <div class="post-nav"><a href="/redis%E4%B8%BB%E4%BB%8E%E5%A4%8D%E5%88%B6/" class="prev" rel="prev" title="redis主从复制"><i class="fas fa-angle-left fa-fw"></i>redis主从复制</a>
            <a href="/activiti%E4%B8%80%E7%AE%80%E4%BB%8B/" class="next" rel="next" title="Activiti（一）简介">Activiti（一）简介<i class="fas fa-angle-right fa-fw"></i></a></div>
</div>
<div id="comments"></div></article></div>
            </main><footer class="footer">
        <div class="footer-container"><div class="footer-line">由 <a href="https://gohugo.io/" target="_blank" rel="noopener noreffer" title="Hugo 0.74.3">Hugo</a> 强力驱动 | 主题 - <a href="https://github.com/dillonzq/LoveIt" target="_blank" rel="noopener noreffer" title="LoveIt 0.2.10"><i class="far fa-kiss-wink-heart fa-fw"></i> LoveIt</a>
                </div><div class="footer-line"><i class="far fa-copyright fa-fw"></i><span itemprop="copyrightYear">2019 - 2021</span><span class="author" itemprop="copyrightHolder">&nbsp;<a href="/" target="_blank">豆是不吃豆豉</a></span></div>
        </div>
    </footer></div>

        <div id="fixed-buttons"><a href="#" id="back-to-top" class="fixed-button" title="回到顶部">
                <i class="fas fa-arrow-up fa-fw"></i>
            </a><a href="#" id="view-comments" class="fixed-button" title="查看评论">
                <i class="fas fa-comment fa-fw"></i>
            </a>
        </div><link rel="stylesheet" href="/lib/katex/katex.min.css"><link rel="stylesheet" href="/lib/katex/copy-tex.min.css"><link rel="stylesheet" href="/lib/cookieconsent/cookieconsent.min.css"><script type="text/javascript" src="/lib/smooth-scroll/smooth-scroll.min.js"></script><script type="text/javascript" src="/lib/autocomplete/autocomplete.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.stemmer.support.min.js"></script><script type="text/javascript" src="/lib/lunr/lunr.zh.min.js"></script><script type="text/javascript" src="/lib/lazysizes/lazysizes.min.js"></script><script type="text/javascript" src="/lib/clipboard/clipboard.min.js"></script><script type="text/javascript" src="/lib/sharer/sharer.min.js"></script><script type="text/javascript" src="/lib/katex/katex.min.js"></script><script type="text/javascript" src="/lib/katex/auto-render.min.js"></script><script type="text/javascript" src="/lib/katex/copy-tex.min.js"></script><script type="text/javascript" src="/lib/katex/mhchem.min.js"></script><script type="text/javascript" src="/lib/cookieconsent/cookieconsent.min.js"></script><script type="text/javascript">window.config={"code":{"copyTitle":"复制到剪贴板","maxShownLines":100},"comment":{},"cookieconsent":{"content":{"dismiss":"同意","link":"了解更多","message":"本网站使用 Cookies 来改善您的浏览体验."},"enable":true,"palette":{"button":{"background":"#f0f0f0"},"popup":{"background":"#1aa3ff"}},"theme":"edgeless"},"math":{"delimiters":[{"display":true,"left":"$$","right":"$$"},{"display":true,"left":"\\[","right":"\\]"},{"display":false,"left":"$","right":"$"},{"display":false,"left":"\\(","right":"\\)"}],"strict":false},"search":{"highlightTag":"em","lunrIndexURL":"/index.json","lunrLanguageCode":"zh","lunrSegmentitURL":"/lib/lunr/lunr.segmentit.js","maxResultLength":10,"noResultsFound":"没有找到结果","snippetLength":50,"type":"lunr"}};</script><script type="text/javascript" src="/js/theme.min.js"></script></body>
</html>
